from fastapi import FastAPI
import uvicorn
from configparser import ConfigParser
from starlette.middleware.cors import CORSMiddleware

app = FastAPI(title="应急资源测试API")

# 跨域 , 允许所有
origins = ["*"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,  #设置允许的origins来源
    allow_credentials=True,
    allow_methods=["*"],  # 设置允许跨域的http方法，比如 get、post、put等。
    allow_headers=["*"]  #允许跨域的headers，可以用来鉴别来源等作用。
)

# default
config = ConfigParser()
config.read('config.ini')
uvicorn_config = config['uvicorn']
uvicorn_host = uvicorn_config['host']
uvicorn_port = int(uvicorn_config['port'])


@app.get("/VideoServer/dahuaDss/getOrg.json", summary= "获取机构列表")
def getOrg():
    return {
        "requestParam": {
            "superiorId": None,
            "chanelId": None,
            "scheme": None
        },
        "requestResult": {
            "result": True,
            "message": None,
            "ortherId": 0,
            "data": [
                {
                    "name": "高栏港应急监控系统",
                    "id": "S4NbecfYB1C62G1ODDS8N4",
                    "devices": [],
                    "childs": [
                        {
                            "name": "汇华管廊",
                            "id": "S4NbecfYB1C62GP23PERN4",
                            "devices": [
                                {
                                    "id": "n9WDBEhrA1C7K2JTN4559F",
                                    "name": "机房",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53LTK",
                                    "name": "运行管理中心",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53M39",
                                    "name": "大堂",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53M4C",
                                    "name": "二楼走廊",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53M92",
                                    "name": "三楼走廊",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MCA",
                                    "name": "西北方向",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53ME0",
                                    "name": "西南方向",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MHT",
                                    "name": "东南方向",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MM5",
                                    "name": "大门",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MPB",
                                    "name": "一楼走廊",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MQD",
                                    "name": "厨房",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K2JTN53MTC",
                                    "name": "运行仓库",
                                    "online": 1
                                }
                            ],
                            "childs": None
                        },
                        {
                            "name": "安检中心",
                            "id": "S4NbecfYB1C7F1917LDCB4",
                            "devices": [
                                {
                                    "id": "n9WDBEhrA1C7K2JTN36II3",
                                    "name": "环岛路-G4-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0PSH",
                                    "name": "风鹰一路-G16-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0Q3K",
                                    "name": "平排四路-G15-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0Q9E",
                                    "name": "南迳中路-G17-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0QH5",
                                    "name": "风鹰南路-G19-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0QLI",
                                    "name": "风鹰南路-G19-球机2",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9S0QTM",
                                    "name": "风鹰南路-G20-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVBLG",
                                    "name": "风鹰南路-G20-球机2",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVBQO",
                                    "name": "风鹰北路-G24-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVC20",
                                    "name": "风鹰北路-G24-球机2",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVC83",
                                    "name": "风鹰北路-G25-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVCFQ",
                                    "name": "风鹰北路-G25-球机2",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9SVCKB",
                                    "name": "南迳东路-G28-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9TTTE5",
                                    "name": "南迳东路-G28-球机2",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9TTTK9",
                                    "name": "南迳东路-G29-球机1",
                                    "online": 1
                                },
                                {
                                    "id": "n9WDBEhrA1C7K6AE9TTTP8",
                                    "name": "南迳东路-G29-球机2",
                                    "online": 1
                                }
                            ],
                            "childs": None
                        },
                        {
                            "name": "高栏港大厦",
                            "id": "S4NbecfYB1C7HMDKDGSRO8",
                            "devices": [
                                {
                                    "id": "n9WDBEhrA1C7K6AE9R28JL",
                                    "name": "IP PTZ Camera",
                                    "online": 1
                                }
                            ],
                            "childs": None
                        }
                    ]
                }
            ]
        }
    }

@app.get("/VideoServer/dahuaDss/getPlayUrl.json", summary= "获取视频实时播放url",str="测试")
def getPlayUrl(chanelId : str, scheme : str):
    return  {
        "requestParam": {
            "superiorId": None,
            "chanelId": chanelId,
            "scheme": scheme
        },
        "requestResult": {
            "result": True,
            "message": None,
            "ortherId": 0,
            "data": "http://10.24.66.247:8050/cam/realmonitor/n9WDBEhrA1C7K2JTN53MM5?subtype=0&streamType=0&token=1594231197_fe413db2d15ab4f134ea64e8eb347db1b53d3eea&mediatype=HLS.m3u8"
        }
    }

def run():
    uvicorn.run("camera_api:app", port=uvicorn_port, reload = True, host=uvicorn_host)

if __name__ == '__main__':
    run()